<?php

class WilliamHillImporter extends Importer{
	
	const URL_FEED = 'http://cachepricefeeds.williamhill.com/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=46&marketSort=CS&filterBIR=N';
	const XML_NAME = 'WilliamHill';
	const CONFIG_NAME = 'WilliamHillConfig';
	const LOG_NAME = 'WilliamHillLog';

	public function execute(){
		$this->loadOdds();
	}
	
	private function loadOdds(){
		$leagueName = $this->config[ $this->league->idLeague ][ 'name' ];
		$xmlLeague = $this->xml->xpath( "/oxip/response/williamhill/class/type[@name='$leagueName']" );
		
		foreach( $xmlLeague as $league ){
			foreach( $league as $oddType ){
				if( strpos( $oddType['name'], $this->oddType->oddType ) ){
					$found = false;
					foreach( $this->config[ $this->league->idLeague ][ 'betTypes' ]
							 [ $this->oddType->idOddType ][ 'notContains' ] as $rule ){
						if( strpos( $oddType['name'], $rule ) ) $found = true;
					}
					if( $found ) continue;
				}else{
					continue;
				}
				
				/* Get teams */
				$splitedNames = explode( ' v ', $oddType['name'] );
				
				$teamName1 = $splitedNames['0'];
				$teamName2 = explode( ' - ', $splitedNames['1'] );
				$teamName2 = $teamName2[0];
				
				$idTeam1 = $this->getTeamsIdByTeamName( $teamName1 );
				if( !$idTeam1 ){
					$this->errorCount++;
					$this->insertLog( "'$teamName1' not found as team in own config\n" );
					continue;
				}
				
				$idTeam2 = $this->getTeamsIdByTeamName( $teamName2 );
				if( !$idTeam2 ){
					$this->errorCount++;
					$this->insertLog( "'$teamName2' not found as team in own config\n" );
					continue;
				}
				
				/* Get match id */
				$match = Matches::model()->findByAttributes( [ 
						'idJourney' => $this->currentJourney->idJourney, 
						'idTeam1' => $idTeam1, 
						'idTeam2' => $idTeam2, 
						'idSeason' => $this->currentSeason->idSeason, 
						'idLeague' => $this->league->idLeague ] );
				
				if( !$match ){
					$this->errorCount++;
					$this->insertLog( "Not match found with idJourney: {$this->currentJourney->idJourney}, idTeam1: $idTeam1,
					 idTeam2: $idTeam2, idSeason: {$this->currentSeason->idSeason}, idLeague: {$this->league->idLeague}\n" );
					continue;
				}
				
				/* Get odds */
				foreach( $oddType as $participant ){
					$result = [];
					preg_match( '/[0-9]+-[0-9]+/', (string)$participant['name'], $result );
					
					/* If participant name contains teamName2, odd is inverted: 5-0 changes to 0-5*/
					if( strpos( (string)$participant[ 'name' ], $teamName2 ) !== false ){
						$score = explode( '-', $result[0] );
						$result = $score[1].'-'.$score[0];
					}else{
						$result = $result[0];
					}
					
					/* Insert Odd */
					$newOdd = new Odds;
					$newOdd->score = $result;
					$newOdd->oddDecimal = (float) $participant[ 'oddsDecimal' ];
					$newOdd->lastUpdateTime = $participant[ 'lastUpdateDate' ].' '.$participant[ 'lastUpdateTime' ];
					$newOdd->idMatch = $match->idMatch;
					$newOdd->idOddType = $this->oddType->idOddType;
					$newOdd->save();
					
					echo "Odd Added: $teamName1 - $teamName2 -> $result - {$participant[ 'oddsDecimal' ]}\n";
				}
			}
		}
	}
}